//格式化时间
function dateFtt(fmt, date) { //author: meizz
    var o = {
        "M+": date.getMonth() + 1,                 //月份
        "d+": date.getDate(),                    //日
        "h+": date.getHours(),                   //小时
        "m+": date.getMinutes(),                 //分
        "s+": date.getSeconds(),                 //秒
        "q+": Math.floor((date.getMonth() + 3) / 3), //季度
        "S": date.getMilliseconds()             //毫秒
    };
    if (/(y+)/.test(fmt))
        fmt = fmt.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    for (var k in o)
        if (new RegExp("(" + k + ")").test(fmt))
            fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
    return fmt;
}

function formatSubscribeTime(cellValue, options, rowObject) {
    var dateStr = "";
    if (cellValue) {
        var crtTime = new Date(cellValue);
        dateStr = dateFtt("yyyy-MM-dd hh:mm:ss", crtTime);
    }
    return dateStr;
}

function auditDeptChange(auditDept){
    if(auditDept === '370402000000') {
        return '市中区交警大队';
    } else if(auditDept === '370403000000') {
        return '薛城区交警大队';
    } else if(auditDept === '370404000000') {
        return '峄城区交警大队';
    } else if(auditDept === '370405000000') {
        return '台儿庄区交警大队';
    } else if(auditDept === '370406000000') {
        return '山亭区交警大队';
    } else if(auditDept === '370481000000') {
        return '滕州市交警大队';
    }
}

$(function () {
    $("#jqGrid").jqGrid({
        url: baseURL + 'cgs/cgspassporapply/list',
        datatype: "json",
        colModel: [
            {label: 'id', name: 'id', index: 'id', width: 50, key: true, hidden: true},
            {label: '申请用户', name: 'applyUserId', index: 'apply_user_id', width: 80},
            {
                label: '申请类型',
                name: 'applyRole',
                index: 'apply_role',
                width: 80,
                formatter: function (value, options, row) {
                    if (value === '01') {
                        return '<span>个人</span>';
                    } else if (value === '02') {
                        return '<span>单位</span>';
                    }
                }
            },
            {label: '通行证id', name: 'passportId', index: 'passport_id', width: 80, hidden: true},
            {label: '通行证名称', name: 'passportName', index: 'passport_name', width: 100},
            {label: '通行时间', name: 'passTime', index: 'pass_time', width: 140, hidden: true},
            // {label: '有效时长', name: 'effectTime', index: 'effect_time', width: 80, hidden: true},
            {label: '审核大队', name: 'auditDept', index: 'audit_dept', width: 80,formatter:function(value,options,row){
                    return auditDeptChange(value);
            }},
            {label: '办理人', name: 'handler', index: 'handler', width: 80, hidden: true},
            {label: '联系电话', name: 'mobile', index: 'mobile', width: 80, hidden: true},
            {label: '身份证号', name: 'idCardNum', index: 'id_card_num', width: 80, hidden: true},
            {label: '开始日期', name: 'beginDate', index: 'begin_date', width: 80},
            {label: '结束日期', name: 'toDate', index: 'to_date', width: 80},
            {label: '运输货物', name: 'goodsId', index: 'goods_id', width: 80, hidden: true},
            {label: '货物名称', name: 'goodsName', index: 'goods_name', width: 80},
            {label: '目的地', name: 'destination', index: 'destination', width: 80,hidden:true},
            {label: '号牌类型', name: 'vehicleType', index: 'vehicle_type', width: 80,formatter:function(value,options,row){
                return dictJudge(value,vm.hpzl);
            }},
            {label: '车牌号码', name: 'vehicleNum', index: 'vehicle_num', width: 80},
            {label: '线路名称', name: 'lineName', index: 'line_name', width: 80, hidden: true},
            {label: '审批线路', name: 'verifyLineName', index: 'verify_line_name', width: 80, hidden: true},
            {label: '申请时间', name: 'createTime', index: 'create_time', width: 80,formatter: formatSubscribeTime},
            {label: '审批状态', name: 'status', index: 'status', width: 80,formatter:function(value,options,row){
                return dictJudge(value, vm.spzt);
            }},
            {label: '审核不通过原因', name: 'statusExplain', index: 'status_explain', width: 80, hidden: true},
            {label: '营业执照', name: 'businessLicense', index: 'business_license', width: 80, hidden: true},
            {label: '行驶证', name: 'travelLicense', index: 'travel_license', width: 80, hidden: true},
            {label: '驾驶证', name: 'drivingLicense', index: 'driving_license', width: 80, hidden: true},
            {label: '危险品押运员证', name: 'goodsScortCard', index: 'goods_scort_card', width: 80, hidden: true},
            {label: '通行申请书', name: 'passportApplication', index: 'passport_application', width: 80, hidden: true},
            {label: '渣土清运审批表', name: 'qualificationProof', index: 'qualification_proof', width: 80, hidden: true},
            {label: '违法处理完成证明', name: 'illegalDealProve', index: 'illegal_deal_prove', width: 80, hidden: true},
            {label: '培训合格证明', name: 'trainingCertificate', index: 'training_certificate', width: 80, hidden: true},
            {label: '创建时间', name: 'createTime', index: 'create_time', width: 80, hidden: true},
            {label: '备注', name: 'remark', index: 'remark', width: 80, hidden: true}
        ],
        viewrecords: true,
        height: 385,
        rowNum: 10,
        rowList: [10, 30, 50],
        rownumbers: true,
        rownumWidth: 25,
        autowidth: true,
        multiselect: true,
        pager: "#jqGridPager",
        jsonReader: {
            root: "page.list",
            page: "page.currPage",
            total: "page.totalPage",
            records: "page.totalCount"
        },
        prmNames: {
            page: "page",
            rows: "limit",
            order: "order"
        },
        gridComplete: function () {
            //隐藏grid底部滚动条
            $("#jqGrid").closest(".ui-jqgrid-bdiv").css({"overflow-x": "hidden"});
        }
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        cgsPassporapply: {},
        hpzl:[],
        shjg:[],
        spzt:[],
        goods:[],
        shenpi:{'status':'4','statusExplain':'','verifyLineName':'','id':''},
        q:{
            applyUserId: '',
            vehicleNum: '',
            createTime: '',
            status: ''
        }
    },
    created:function(){
        $.get(baseURL+"/sys/dict/select/plateNumberType,passport_verify,passporapply,goods_name",function (data) {
            vm.hpzl = data.dict.plateNumberType;
            vm.shjg = data.dict.passport_verify;
            vm.spzt = data.dict.passporapply;
            vm.goods = data.dict.goods_name;
        });
    },
    methods: {
        query: function () {
            vm.reload();
        },
        reset: function () {
            vm.q.applyUserId = '';
            vm.q.vehicleNum = '';
            vm.q.createTime = '';
            vm.q.status = '';
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.cgsPassporapply = {};
        },
        look: function (event) {
            var id = getSelectedRow();
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "查看详情";

            vm.getInfo(id)
        },
        executeAudit: function () {
            if(vm.shenpi.status === '2' && $.isEmptyObject(vm.shenpi.statusExplain)){
                alert("请填写拒绝事由");
                return ;
            }
            if(vm.shenpi.status === '4' && $.isEmptyObject(vm.shenpi.verifyLineName)){
                alert("请填写批复路线");
                return ;
            }
            vm.shenpi.id=vm.cgsPassporapply.id;
            var url = "cgs/cgspassporapply/update";
            $.ajax({
                type: "POST",
                url: baseURL + url,
                contentType: "application/json",
                data: JSON.stringify(vm.shenpi),
                success: function (r) {
                    if (r.code === 0) {
                        alert('操作成功', function (index) {
                            $("#passporapplyModal").modal('hide');
                            vm.reload();
                        });
                    } else {
                        alert(r.msg);
                    }
                }
            });
        },
        getInfo: function (id) {
            $.get(baseURL + "cgs/cgspassporapply/info/" + id, function (r) {
                r.cgsPassporapply.auditDept = auditDeptChange(r.cgsPassporapply.auditDept);
                vm.cgsPassporapply = r.cgsPassporapply;

            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData:{
                    'applyUserId': vm.q.applyUserId,
                    'vehicleNum': vm.q.vehicleNum,
                    'createTime': vm.q.createTime,
                    'status': vm.q.status,
                },
                page: page
            }).trigger("reloadGrid");
        }
    }
});